package com.li.controller;


import com.li.pojo.ReturnMessage;
import com.li.pojo.User;

import com.li.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.Map;


@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public ReturnMessage login(User user){
        ReturnMessage returnMessage= new ReturnMessage();
        user=userService.login(user.getName(),user.getPassword());
        if (user==null){
            returnMessage.setState("400");
            returnMessage.setMsg("用户名或密码错误");
            return returnMessage;
        }
        returnMessage.setMsg("登录成功");
        returnMessage.setData(user);
        return returnMessage;
    }

    @RequestMapping("/addUser")
    public ReturnMessage userAdd(User user){
        ReturnMessage returnMessage = new ReturnMessage();

        int f = userService.addUser(user);
        if (f==0){
            returnMessage.setMsg("用户已存在");
            returnMessage.setState("400");
            return returnMessage;
        }
        returnMessage.setMsg(user.getName()+"添加成功!");
        returnMessage.setData(user);
        return returnMessage;
    }

    @RequestMapping("/selectUsers")
    public ReturnMessage selectUsers(String name,String id,Integer pageSize,Integer page){
        ReturnMessage returnMessage = new ReturnMessage();
        List<User> users= userService.selectUsers(id,name,page,pageSize);
        long count = userService.count(id,name);
        returnMessage.setData(users);
        returnMessage.setMsg("获取用户数据成功");
        returnMessage.setOther(count);
        System.out.println(returnMessage);
        return returnMessage;
    }
    @RequestMapping("/deleteUsers")
    public ReturnMessage deleteUser(@RequestBody List<Integer> deleteList){
        ReturnMessage returnMessage = new ReturnMessage();
        int f = userService.deleteUsers(deleteList);
        System.out.println("删除");
        System.out.println(deleteList);
        returnMessage.setData(deleteList);
        returnMessage.setMsg("删除成功");
        return returnMessage;
    }
    @RequestMapping("/updateUser")
    public ReturnMessage updateUser(User user){
        ReturnMessage returnMessage = new ReturnMessage();
        System.out.println("更新:");
        System.out.println(user);
        userService.updateUser(user);
        returnMessage.setMsg("用户："+user.getName()+"更新成功!");
        returnMessage.setData(user);
        return returnMessage;
    }
    @RequestMapping("selectAllUsers")
    public List<User> selectAllUsers(){
        return userService.selectAllUsers();
    }
}
